<?php
@define('__PNADDRESSBOOK__','pnAddressBook');
// $Id: pnadmin.php,v 1.2 2003/05/22 18:42:51 garrett Exp $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2002 by the PostNuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WIthOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Thomas Smiatek
// Purpose of file:  pnAddressBook administration display functions
// ----------------------------------------------------------------------

//=========================================================================
//  Load the API Functions
//=========================================================================
pnModAPILoad(__PNADDRESSBOOK__,'admin');

//=========================================================================
//  the main administration function
//=========================================================================
function pnAddressBook_admin_main() {

    $output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

    // Add menu to output - it helps if all of the module pages have a standard
    // menu at their head to aid in navigation

    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$msg = pnVarCleanFromInput('msg');
	$output->Linebreak(1);
	$output->Title($msg);

    // Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Modify the settings
//=========================================================================
function pnAddressBook_admin_modifyconfig() {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

    $output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$msg = pnVarCleanFromInput('msg');
	$output->Title($msg);

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

    // Start form
    $output->FormStart(pnModURL(__PNADDRESSBOOK__, 'admin', 'updateconfig'));

    // Add an authorisation ID
    $output->FormHidden('authid', pnSecGenAuthKey());

	// get access rights
	switch (pnModGetVar(__PNADDRESSBOOK__, 'guestmode')) {
		case '0':
			$guestmode_1 = 0;
			$guestmode_2 = 0;
			$guestmode_3 = 0;
			break;
		case '1':
			$guestmode_1 = 0;
			$guestmode_2 = 0;
			$guestmode_3 = 1;
			break;
		case '2':
			$guestmode_1 = 0;
			$guestmode_2 = 1;
			$guestmode_3 = 0;
			break;
		case '3':
			$guestmode_1 = 0;
			$guestmode_2 = 1;
			$guestmode_3 = 1;
			break;
		case '4':
			$guestmode_1 = 1;
			$guestmode_2 = 0;
			$guestmode_3 = 0;
			break;
		case '5':
			$guestmode_1 = 1;
			$guestmode_2 = 0;
			$guestmode_3 = 1;
			break;
		case '6':
			$guestmode_1 = 1;
			$guestmode_2 = 1;
			$guestmode_3 = 0;
			break;
		case '7':
			$guestmode_1 = 1;
			$guestmode_2 = 1;
			$guestmode_3 = 1;
			break;
	}

	switch (pnModGetVar(__PNADDRESSBOOK__, 'usermode')) {
		case '0':
			$usermode_1 = 0;
			$usermode_2 = 0;
			$usermode_3 = 0;
			break;
		case '1':
			$usermode_1 = 0;
			$usermode_2 = 0;
			$usermode_3 = 1;
			break;
		case '2':
			$usermode_1 = 0;
			$usermode_2 = 1;
			$usermode_3 = 0;
			break;
		case '3':
			$usermode_1 = 0;
			$usermode_2 = 1;
			$usermode_3 = 1;
			break;
		case '4':
			$usermode_1 = 1;
			$usermode_2 = 0;
			$usermode_3 = 0;
			break;
		case '5':
			$usermode_1 = 1;
			$usermode_2 = 0;
			$usermode_3 = 1;
			break;
		case '6':
			$usermode_1 = 1;
			$usermode_2 = 1;
			$usermode_3 = 0;
			break;
		case '7':
			$usermode_1 = 1;
			$usermode_2 = 1;
			$usermode_3 = 1;
			break;
	}

    // Start the table that holds the information to be modified.
    $output->Text(pnAddressBook_themetable('start'));
	$output->Linebreak(1);
	$output->Text('<table align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');

	// note
	$output->TableRowStart();
	$output->Text('<td colspan="4" align="center" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_GRANTNOTE));
	$output->Linebreak(2);
	$output->TableColEnd();
	$output->TableRowEnd();

	// headline
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_GRANTLEVEL).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(pnVarPrepHTMLDisplay(_pnAB_VIEW));
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(pnVarPrepHTMLDisplay(_pnAB_CREATE));
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(pnVarPrepHTMLDisplay(_pnAB_EDITDELETE));
    $output->TableColEnd();
	$output->TableRowEnd();

	// Access rights for unregistered users
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_GUESTMODE));
	$output->TableColEnd();
    $output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('guestmode_1', $guestmode_1);
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('guestmode_2', $guestmode_2);
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('guestmode_3', $guestmode_3);
    $output->TableColEnd();
	$output->TableRowEnd();

	// Access rights for registered users
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_USERMODE));
	$output->TableColEnd();
    $output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('usermode_1', $usermode_1);
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('usermode_2', $usermode_2);
	$output->TableColEnd();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('usermode_3', $usermode_3);
    $output->TableColEnd();
	$output->TableRowEnd();

	// empty row
	$output->TableRowStart();
	$output->Text('<td colspan="4" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->Linebreak(1);
    $output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_OTHERSTUFF).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	// Title of Address Book
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_TITLE));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('abtitle', pnModGetVar(__PNADDRESSBOOK__, 'abtitle'), 30, 60);
	$output->TableColEnd();
	$output->TableRowEnd();

	//Default sortorder 1
	$sortCols = explode(',',pnModGetVar(__PNADDRESSBOOK__, 'sortorder_1'));
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_SORTORDER_1));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$sortdata_1[] = array('id'=>'adr_sortname', 'name'=>_pnAB_NAME);
	$sortdata_1[] = array('id'=>'adr_title', 'name'=>_pnAB_TITLE);
	$sortdata_1[] = array('id'=>'adr_sortcompany', 'name'=>_pnAB_COMPANY);
	$sortdata_1[] = array('id'=>'adr_zip', 'name'=>_pnAB_ZIP);
	$sortdata_1[] = array('id'=>'adr_city', 'name'=>_pnAB_CITY);
	$sortdata_1[] = array('id'=>'adr_state', 'name'=>_pnAB_STATE);
	$sortdata_1[] = array('id'=>'adr_country', 'name'=>_pnAB_COUNTRY);
	$custom_tab = pnModGetVar(__PNADDRESSBOOK__,'custom_tab');
	if ((!empty($custom_tab)) && ($custom_tab != '')) {
		$cus_fields = pnModAPIFunc(__PNADDRESSBOOK__,'admin','customFieldInformation',array('id'=>$id));
		foreach($cus_fields as $cus) {
			$the_name = 'adr_custom_'.$cus['nr'];
			$sortdata_1[] = array('id'=>$the_name, 'name'=>pnVarPrepHTMLDisplay($cus['name']));
		}
	}
	$output->FormSelectMultiple('sortdata_1',$sortdata_1, 0, 1,$sortCols[0], '');
	$output->Text(' | ');
	$sortdata_2[] = array('id'=>'adr_sortname', 'name'=>_pnAB_NAME);
	$sortdata_2[] = array('id'=>'adr_title', 'name'=>_pnAB_TITLE);
	$sortdata_2[] = array('id'=>'adr_sortcompany', 'name'=>_pnAB_COMPANY);
	$sortdata_2[] = array('id'=>'adr_zip', 'name'=>_pnAB_ZIP);
	$sortdata_2[] = array('id'=>'adr_city', 'name'=>_pnAB_CITY);
	$sortdata_2[] = array('id'=>'adr_state', 'name'=>_pnAB_STATE);
	$sortdata_2[] = array('id'=>'adr_country', 'name'=>_pnAB_COUNTRY);
	if ((!empty($custom_tab)) && ($custom_tab != '')) {
		$cus_fields2 = pnModAPIFunc(__PNADDRESSBOOK__,'admin','customFieldInformation',array('id'=>$id));
		foreach($cus_fields2 as $cus) {
			$the_name = 'adr_custom_'.$cus['nr'];
			$sortdata_2[] = array('id'=>$the_name, 'name'=>pnVarPrepHTMLDisplay($cus['name']));
		}
	}
	$output->FormSelectMultiple('sortdata_2',$sortdata_2, 0, 1,$sortCols[1], '');
	$output->TableColEnd();
	$output->TableRowEnd();

	//Default sortorder 2
	$sortCols = explode(',',pnModGetVar(__PNADDRESSBOOK__, 'sortorder_2'));
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_SORTORDER_2));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$sortdata_3[] = array('id'=>'adr_sortname', 'name'=>_pnAB_NAME);
	$sortdata_3[] = array('id'=>'adr_title', 'name'=>_pnAB_TITLE);
	$sortdata_3[] = array('id'=>'adr_sortcompany', 'name'=>_pnAB_COMPANY);
	$sortdata_3[] = array('id'=>'adr_zip', 'name'=>_pnAB_ZIP);
	$sortdata_3[] = array('id'=>'adr_city', 'name'=>_pnAB_CITY);
	$sortdata_3[] = array('id'=>'adr_state', 'name'=>_pnAB_STATE);
	$sortdata_3[] = array('id'=>'adr_country', 'name'=>_pnAB_COUNTRY);
	if ((!empty($custom_tab)) && ($custom_tab != '')) {
		$cus_fields3 = pnModAPIFunc(__PNADDRESSBOOK__,'admin','customFieldInformation',array('id'=>$id));
		foreach($cus_fields3 as $cus) {
			$the_name = 'adr_custom_'.$cus['nr'];
			$sortdata_3[] = array('id'=>$the_name, 'name'=>pnVarPrepHTMLDisplay($cus['name']));
		}
	}
	$output->FormSelectMultiple('sortdata_3',$sortdata_3, 0, 1,$sortCols[0], '');
	$output->Text(' | ');
	$sortdata_4[] = array('id'=>'adr_sortname', 'name'=>_pnAB_NAME);
	$sortdata_4[] = array('id'=>'adr_title', 'name'=>_pnAB_TITLE);
	$sortdata_4[] = array('id'=>'adr_sortcompany', 'name'=>_pnAB_COMPANY);
	$sortdata_4[] = array('id'=>'adr_zip', 'name'=>_pnAB_ZIP);
	$sortdata_4[] = array('id'=>'adr_city', 'name'=>_pnAB_CITY);
	$sortdata_4[] = array('id'=>'adr_state', 'name'=>_pnAB_STATE);
	$sortdata_4[] = array('id'=>'adr_country', 'name'=>_pnAB_COUNTRY);
	if ((!empty($custom_tab)) && ($custom_tab != '')) {
		$cus_fields4 = pnModAPIFunc(__PNADDRESSBOOK__,'admin','customFieldInformation',array('id'=>$id));
		foreach($cus_fields4 as $cus) {
			$the_name = 'adr_custom_'.$cus['nr'];
			$sortdata_4[] = array('id'=>$the_name, 'name'=>pnVarPrepHTMLDisplay($cus['name']));
		}
	}
	$output->FormSelectMultiple('sortdata_4',$sortdata_4, 0, 1,$sortCols[1], '');
	$output->TableColEnd();
	$output->TableRowEnd();

	// Name display / sortorder
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_NAME_ORDER));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$temp1 = pnVarPrepHTMLDisplay(_pnAB_LASTNAME).', '.pnVarPrepHTMLDisplay(_pnAB_FIRSTNAME);
	$temp2 = pnVarPrepHTMLDisplay(_pnAB_FIRSTNAME).' '.pnVarPrepHTMLDisplay(_pnAB_LASTNAME);
	$name_order_data[] = array('id'=>0, 'name'=>$temp1);
	$name_order_data[] = array('id'=>1, 'name'=>$temp2);
	$output->FormSelectMultiple('name_order',$name_order_data, 0, 1,pnModGetVar(__PNADDRESSBOOK__, 'name_order'), '');
	$output->TableColEnd();
	$output->TableRowEnd();

	// Special character replacement in sort columns
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_SPECIAL_CHARS));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('special_chars_1', pnModGetVar(__PNADDRESSBOOK__, 'special_chars_1'), 12, 24);
	$output->Text(' => ');
	$output->FormText('special_chars_2', pnModGetVar(__PNADDRESSBOOK__, 'special_chars_2'), 12, 24);
	$output->TableColEnd();
	$output->TableRowEnd();

	// Allow global entries
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_GLOBALPROTECT));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('globalprotect', pnModGetVar(__PNADDRESSBOOK__, 'globalprotect'));
	$output->TableColEnd();
	$output->TableRowEnd();

	// Use prefixes
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_SHOWPREFIX));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('use_prefix', pnModGetVar(__PNADDRESSBOOK__, 'use_prefix'));
	$output->TableColEnd();
	$output->TableRowEnd();

	// Use images
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_SHOWIMG));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('use_img', pnModGetVar(__PNADDRESSBOOK__, 'use_img'));
	$output->TableColEnd();
	$output->TableRowEnd();

	// Disable the menu
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_MENU_OFF));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$menu_off_data[] = array('id'=>0, 'name'=>pnVarPrepHTMLDisplay(_pnAB_HIDENOTHING));
	$menu_off_data[] = array('id'=>1, 'name'=>pnVarPrepHTMLDisplay(_pnAB_HIDEALL));
	$menu_off_data[] = array('id'=>2, 'name'=>pnVarPrepHTMLDisplay(_pnAB_HIDEGUESTS));
	$output->FormSelectMultiple('menu_off',$menu_off_data, 0, 1,pnModGetVar(__PNADDRESSBOOK__, 'menu_off'), '');
	$output->TableColEnd();
	$output->TableRowEnd();

	// Disable second main menu line
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_MENU_SEMI));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('menu_semi', pnModGetVar(__PNADDRESSBOOK__, 'menu_semi'));
	$output->TableColEnd();
	$output->TableRowEnd();

	// Show zip before city
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_ZIPBEFORECITY));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('zipbeforecity', pnModGetVar(__PNADDRESSBOOK__, 'zipbeforecity'));
	$output->TableColEnd();
	$output->TableRowEnd();

	// Number of items to display per page
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_ITEMSPERPAGE));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('itemsperpage', pnModGetVar(__PNADDRESSBOOK__, 'itemsperpage'), 4, 4);
	$output->TableColEnd();
	$output->TableRowEnd();

	// Hide Copyright
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_HIDECOPYRIGHT));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormCheckbox('hidecopyright', pnModGetVar(__PNADDRESSBOOK__, 'hidecopyright'));
	$output->TableColEnd();
	$output->TableRowEnd();

	// empty row
	$output->TableRowStart();
	$output->Text('<td colspan="4" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->Linebreak(1);
    $output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_CUSTOMLABEL).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	// Custom Tab
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_CUSTOM_TAB));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('custom_tab', pnModGetVar(__PNADDRESSBOOK__, 'custom_tab'), 30, 60);
	$output->TableColEnd();
	$output->TableRowEnd();

	// Custom textareawidth
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_CUSTOM_TEXTAREAWIDTH));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('textareawidth', pnModGetVar(__PNADDRESSBOOK__, 'textareawidth'), 4, 4);
	$output->TableColEnd();
	$output->TableRowEnd();

	// Custom dateformat
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_CUSTOM_DATEFORMAT));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$formdata1[] = array('id'=>0, 'name'=>pnVarPrepForDisplay(_pnAB_DATEFORMAT_1));
	$formdata1[] = array('id'=>1, 'name'=>pnVarPrepForDisplay(_pnAB_DATEFORMAT_2));
	$output->FormSelectMultiple('dateformat',$formdata1, 0, 1,pnModGetVar(__PNADDRESSBOOK__, 'dateformat'), '');
	$output->TableColEnd();
	$output->TableRowEnd();

	// Custom numeric format
	$output->TableRowStart();
	$output->Text('<td align="left" valign="middle" bgcolor="'.$bc1.'">');
    $output->Text(pnVarPrepHTMLDisplay(_pnAB_CUSTOM_NUMFORMAT));
	$output->TableColEnd();
    $output->Text('<td colspan="3" align="left" valign="middle" bgcolor="'.$bc1.'">');
	$formdata2[] = array('id'=>'9,999.99', 'name'=>'9,999.99');
	$formdata2[] = array('id'=>'9.999,99', 'name'=>'9.999,99');
	$output->FormSelectMultiple('numformat',$formdata2, 0, 1,pnModGetVar(__PNADDRESSBOOK__, 'numformat'), '');
	$output->TableColEnd();
	$output->TableRowEnd();

	$output->Text('</table>');
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('end'));

	// End form
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
    $output->FormSubmit(pnVarPrepHTMLDisplay(_pnAB_PNADDRESSBOOKUPDATE));
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	// Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Update the settings
//=========================================================================
function pnAddressBook_admin_updateconfig() {

    $output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$output->Linebreak(1);

    // Confirm authorisation code.
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        return false;
    }

    // Get parameters from whatever input we need.
    $guestmode_1 = pnVarCleanFromInput('guestmode_1');
	$guestmode_2 = pnVarCleanFromInput('guestmode_2');
	$guestmode_3 = pnVarCleanFromInput('guestmode_3');
	$usermode_1 = pnVarCleanFromInput('usermode_1');
	$usermode_2 = pnVarCleanFromInput('usermode_2');
	$usermode_3 = pnVarCleanFromInput('usermode_3');
	$globalprotect = pnVarCleanFromInput('globalprotect');
	$menu_off = pnVarCleanFromInput('menu_off');
	$zipbeforecity = pnVarCleanFromInput('zipbeforecity');
	$abtitle = pnVarCleanFromInput('abtitle');
	$custom_tab = pnVarCleanFromInput('custom_tab');
	$itemsperpage = pnVarCleanFromInput('itemsperpage');
	$hidecopyright = pnVarCleanFromInput('hidecopyright');
	$use_prefix = pnVarCleanFromInput('use_prefix');
	$use_img = pnVarCleanFromInput('use_img');
	$textareawidth = pnVarCleanFromInput('textareawidth');
	$dateformat = pnVarCleanFromInput('dateformat');
	$numformat = pnVarCleanFromInput('numformat');
	$sortdata_1 = pnVarCleanFromInput('sortdata_1');
	$sortdata_2 = pnVarCleanFromInput('sortdata_2');
	$sortdata_3 = pnVarCleanFromInput('sortdata_3');
	$sortdata_4 = pnVarCleanFromInput('sortdata_4');
	$menu_semi = pnVarCleanFromInput('menu_semi');
	$name_order = pnVarCleanFromInput('name_order');
	$special_chars_1 = pnVarCleanFromInput('special_chars_1');
	$special_chars_2 = pnVarCleanFromInput('special_chars_2');

    // Update module variables.
    if (!isset($abtitle)) {
        $abtitle = '';
    }
	if (!isset($globalprotect)) {
        $globalprotect = 0;
    }
	if (!isset($hidecopyright)) {
        $hidecopyright = 0;
    }
	if (!isset($use_prefix)) {
        $use_prefix = 0;
    }
	if (!isset($use_img)) {
        $use_img = 0;
    }
	if (!isset($menu_off)) {
        $menu_off = 0;
    }
	if (!isset($menu_semi)) {
        $menu_semi = 0;
    }
	if (!isset($name_order)) {
        $name_order = 0;
    }
	if (!isset($zipbeforecity)) {
        $zipbeforecity = 0;
    }
	if (!isset($itemsperpage)) {
        $itemsperpage = 30;
    }
	if (!isset($custom_tab)) {
        $custom_tab = '';
    }
	if (!isset($special_chars_1)) {
        $special_chars_1 = '';
    }
	if (!isset($special_chars_2)) {
        $special_chars_2 = '';
    }
	if (!isset($textareawidth)) {
        $textareawidth = 60;
    }
	if (!isset($dateformat)) {
        $dateformat = 0;
    }
	if (!isset($numformat)) {
        $numformat = '9,999.99';
    }
	$guestmode = 0;
	if (isset($guestmode_1)) {
		$guestmode += 4;
	}
	if (isset($guestmode_2)) {
		$guestmode += 2;
	}
	if (isset($guestmode_3)) {
		$guestmode += 1;
	}

	$usermode = 0;
	if (isset($usermode_1)) {
		$usermode += 4;
	}
	if (isset($usermode_2)) {
		$usermode += 2;
	}
	if (isset($usermode_3)) {
		$usermode += 1;
	}
	if ($globalprotect == 1) {
		if (($guestmode != 0) || ($usermode != 7)) {
			$guestmode = 0;
			$usermode = 7;
			$error = pnVarPrepHTMLDisplay(_pnAB_GLOBALPROTECTERROR);
		}
	}
	if ($guestmode > $usermode) {
		$usermode = $guestmode;
		$error = pnVarPrepHTMLDisplay(_pnAB_GRANTERROR);
	}

	pnModSetVar(__PNADDRESSBOOK__, 'globalprotect', $globalprotect);
	pnModSetVar(__PNADDRESSBOOK__, 'menu_off', $menu_off);
    pnModSetVar(__PNADDRESSBOOK__, 'itemsperpage', $itemsperpage);
	pnModSetVar(__PNADDRESSBOOK__, 'guestmode', $guestmode);
	pnModSetVar(__PNADDRESSBOOK__, 'usermode', $usermode);
	pnModSetVar(__PNADDRESSBOOK__, 'custom_tab', $custom_tab);
	pnModSetVar(__PNADDRESSBOOK__, 'zipbeforecity', $zipbeforecity);
	pnModSetVar(__PNADDRESSBOOK__, 'abtitle', $abtitle);
	pnModSetVar(__PNADDRESSBOOK__, 'hidecopyright', $hidecopyright);
	pnModSetVar(__PNADDRESSBOOK__, 'use_prefix', $use_prefix);
	pnModSetVar(__PNADDRESSBOOK__, 'use_img', $use_img);
	pnModSetVar(__PNADDRESSBOOK__, 'textareawidth', $textareawidth);
	pnModSetVar(__PNADDRESSBOOK__, 'dateformat', $dateformat);
	pnModSetVar(__PNADDRESSBOOK__, 'numformat', $numformat);
	pnModSetVar(__PNADDRESSBOOK__, 'menu_semi', $menu_semi);
	pnModSetVar(__PNADDRESSBOOK__, 'name_order', $name_order);

	if ($sortdata_1 == $sortdata_2) {
		if ($error) {
			$error .= '<br>'.pnVarPrepHTMLDisplay(_pnAB_SORTERROR_1);
		}
		else {
			$error = pnVarPrepHTMLDisplay(_pnAB_SORTERROR_1);
		}
	}
	else {
		$s_1 = $sortdata_1.','.$sortdata_2;
		pnModSetVar(__PNADDRESSBOOK__, 'sortorder_1', $s_1);
	}
	if ($sortdata_3 == $sortdata_4) {
		if ($error) {
			$error .= '<br>'.pnVarPrepHTMLDisplay(_pnAB_SORTERROR_2);
		}
		else {
			$error = pnVarPrepHTMLDisplay(_pnAB_SORTERROR_2);
		}
	}
	else {
		$s_2 = $sortdata_3.','.$sortdata_4;
		pnModSetVar(__PNADDRESSBOOK__, 'sortorder_2', $s_2);
	}

	if (strlen($special_chars_1) != strlen($special_chars_2)) {
		$error .= '<br>'.pnVarPrepHTMLDisplay(_pnAB_SPECIAL_CHARS_ERROR);
	}
	else {
		pnModSetVar(__PNADDRESSBOOK__, 'special_chars_1', $special_chars_1);
		pnModSetVar(__PNADDRESSBOOK__, 'special_chars_2', $special_chars_2);
	}
	$msg = pnVarPrepHTMLDisplay(_pnAB_CONF_pnAB_SUCCESS);
	if (isset($error)) { $msg .= ' - '.$error; }

	$args=array('msg'=>$msg);

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'main',$args));

    // Return
    return true;
}

//=========================================================================
//  Category form
//=========================================================================
function pnAddressBook_admin_categories($args) {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }
    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$msg = pnVarCleanFromInput('msg');
	if ($msg) { $output->Text('<div align="center">'.$msg.'</div>');}
	else {$output->Linebreak(1); }

    $cats = pnModAPIFunc(__PNADDRESSBOOK__,'admin','getCategories');
    if(!is_array($cats)) {
        $output->Text($cats);
        return $output->GetOutput();
    }

	// Start form
    $output->FormStart(pnModURL(__PNADDRESSBOOK__, 'admin', 'updatecategories'));

    // Add an authorisation ID
    $output->FormHidden('authid', pnSecGenAuthKey());

	$output->Text(pnAddressBook_themetable('start'));
	$output->Linebreak(1);
	$output->Text('<table align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_CAT_NAME).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_CAT_DELETE).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	foreach($cats as $cat) {
		$output->TableRowStart();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormText('name[]',$cat['name'],20,30);
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormHidden('id[]',$cat['nr']);
		$output->FormCheckbox('del[]',false,$cat['nr']);
		$output->TableColEnd();
		$output->TableRowEnd();
	}

	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('newname','',20,30);
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(_pnAB_CAT_NEW);
	$output->TableColEnd();
	$output->TableRowEnd();

	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('end'));

	// End form
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
    $output->FormSubmit(pnVarPrepHTMLDisplay(_pnAB_PNADDRESSBOOKUPDATE));
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	// Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Category update
//=========================================================================
function pnAddressBook_admin_updatecategories() {

	$output = new pnHTML();

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	list($id,$del,$name,$newname) = pnVarCleanFromInput('id','del','name','newname');
	if(is_array($del)) {
        $dels = implode(',',$del);
    }

	$modID = $modName = array();

	if(isset($id)) {
		foreach($id as $k=>$i) {
        	$found = false;
        	if(count($del)) {
            	foreach($del as $d) {
                	if($i == $d) {
                    	$found = true;
                    	break;
                	}
            	}
        	}
        	if(!$found) {
            	array_push($modID,$i);
            	array_push($modName,$name[$k]);
            }
    	}
	}

	$pntable = pnDBGetTables();
	$cat_table = $pntable[pnaddressbook_categories];
	$cat_column = $pntable['pnaddressbook_categories_column'];

	$updates = array();
    foreach($modID as $k=>$id) {
    array_push($updates,"UPDATE $cat_table
                             SET $cat_column[name]='".pnVarPrepForStore($modName[$k])."'
                             WHERE $cat_column[nr]=$id");
	}

	$error = '';

	if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','updateCategories',array('updates'=>$updates))) {
    	if (empty($error)) { $error .= 'UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		else { $error .= ' - UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
	}

	$delete = "DELETE FROM $cat_table WHERE $cat_column[nr] IN ($dels)";
	if(isset($dels)) {
        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','deleteCategories',array('delete'=>$delete))) {
            if (empty($error)) { $error .= 'DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
        }
    }

	if( (isset($newname)) && ($newname != '') ) {
        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','addCategories',array('name'=>$newname))) {
            if (empty($error)) { $error .= 'INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		}
    }

	$args=array('msg'=>$error);

	pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'categories',$args));
	return true;
}

//=========================================================================
//  Custom field form
//=========================================================================
function pnAddressBook_admin_customfields($args) {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }
	$authid = pnSecGenAuthKey();

    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$msg = pnVarCleanFromInput('msg');
	if ($msg) { $output->Text('<div align="center">'.$msg.'</div>');}
	else {$output->Linebreak(1); }

    $custs = pnModAPIFunc(__PNADDRESSBOOK__,'admin','getCustomfields');
    if(!is_array($custs)) {
        $output->Text($custs);
        return $output->GetOutput();
    }

	// Start form
    $output->FormStart(pnModURL(__PNADDRESSBOOK__, 'admin', 'updatecustomfields'));

    // Add an authorisation ID
    $output->FormHidden('authid', pnSecGenAuthKey());

	$output->Text(pnAddressBook_themetable('start'));
	$output->Linebreak(1);
	$output->Text('<table align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_CUSTOMLABEL).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_DATATYPE).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_CAT_DELETE).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_ORDER).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	$formdata[] = array('id'=>'varchar(60) default NULL', 'name'=>' Text, 60 chars, 1 line');
	$formdata[] = array('id'=>'varchar(120) default NULL', 'name'=>'Text, 120 chars, 2 lines');
	$formdata[] = array('id'=>'varchar(240) default NULL', 'name'=>'Text, 240 chars, 4 lines');
	$formdata[] = array('id'=>'int default NULL', 'name'=>'Integer numbers');
    $formdata[] = array('id'=>'decimal(10,2) default NULL', 'name'=>'Decimal numbers');
//gehBEGIN
    $formdata[] = array('id'=>'int(1) default NULL', 'name'=>'Checkbox'); //gehINSERT
//gehEND
	$formdata[] = array('id'=>'date default NULL', 'name'=>'Date');
	$formdata[] = array('id'=>'tinyint default NULL', 'name'=>'Blank line');
	$formdata[] = array('id'=>'smallint default NULL', 'name'=>'Horizontal rule');

	foreach($custs as $cust) {
		$output->TableRowStart();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormText('name[]',$cust['name'],20,30);
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormSelectMultiple('cus_type[]',$formdata, 0, 1, $cust['type'], '');
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormHidden('id[]',$cust['nr']);
		if ($cust['nr'] < 5) {
			$output->FormHidden('del[]',false);
		}
		else {
			$output->FormCheckbox('del[]',false,$cust['nr']);
		}
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		if ($cust['position'] == '1') {
			$down = $output->URL(pnModURL(__PNADDRESSBOOK__,'admin','decCustomfields',array('id' => $cust['nr'],'authid' => $authid)),'<img src="modules/'.__PNADDRESSBOOK__.'/pnimages/down.gif" alt="'.pnVarPrepHTMLDisplay(_pnAB_DOWN).'" border="0" hspace="4">');

		}
		else {
			if ($cust['position'] == sizeof($custs)) {
				$up = $output->URL(pnModURL(__PNADDRESSBOOK__,'admin','incCustomfields',array('id' => $cust['nr'],'authid' => $authid)),'<img src="modules/'.__PNADDRESSBOOK__.'/pnimages/up.gif" alt="'.pnVarPrepHTMLDisplay(_pnAB_UP).'" border="0" hspace="4">');
			}
			else {
				$up = $output->URL(pnModURL(__PNADDRESSBOOK__,'admin','incCustomfields',array('id' => $cust['nr'],'authid' => $authid)),'<img src="modules/'.__PNADDRESSBOOK__.'/pnimages/up.gif" alt="'.pnVarPrepHTMLDisplay(_pnAB_UP).'" border="0" hspace="4">');
				$down = $output->URL(pnModURL(__PNADDRESSBOOK__,'admin','decCustomfields',array('id' => $cust['nr'],'authid' => $authid)),'<img src="modules/'.__PNADDRESSBOOK__.'/pnimages/down.gif" alt="'.pnVarPrepHTMLDisplay(_pnAB_DOWN).'" border="0" hspace="4">');
			}
		}
		$output->TableColEnd();
		$output->TableRowEnd();
	}

	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('newname','',20,30);
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormSelectMultiple('newtype',$formdata, 0, 1,'', '');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(_pnAB_CAT_NEW);
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'"><br>');
	$output->TableColEnd();
	$output->TableRowEnd();

	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('end'));

	// End form
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
    $output->FormSubmit(pnVarPrepHTMLDisplay(_pnAB_PNADDRESSBOOKUPDATE));
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	// Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Custom field update
//=========================================================================

function pnAddressBook_admin_updatecustomfields() {

	$output = new pnHTML();

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	list($id,$del,$name,$cus_type,$newname,$newtype) = pnVarCleanFromInput('id','del','name','cus_type','newname','newtype');
	if(is_array($del)) {
        $dels = implode(',',$del);
    }
	$modID = $modName = array();
	$modType = array();
	$modDel = array();
	$modDelType = array();

	if(isset($id)) {
		foreach($id as $k=>$i) {
        	$found = false;
        	if(count($del)) {
            	foreach($del as $d) {
                	if($i == $d) {
                    	$found = true;
						array_push($modDel,$i);
						array_push($modDelType,$cus_type[$k]);
                    	break;
                	}
            	}
        	}
        	if(!$found) {
            	array_push($modID,$i);
            	array_push($modName,$name[$k]);
				array_push($modType,$cus_type[$k]);
            }
    	}
	}

	$pntable = pnDBGetTables();
	$cus_table = $pntable['pnaddressbook_customfields'];
	$cus_column = $pntable['pnaddressbook_customfields_column'];
	$adr_table = $pntable['pnaddressbook_address'];
	$adr_column = $pntable['pnaddressbook_address_column'];

	$updates = array();

	foreach($modID as $k=>$id) {
    	array_push($updates,"UPDATE $cus_table
                             SET $cus_column[name]='".pnVarPrepForStore($modName[$k])."',
							 $cus_column[type]='".pnVarPrepForStore($modType[$k])."'
                             WHERE $cus_column[nr]=$id");
		if (($modType[$k] != 'smallint default NULL') && ($modType[$k] != 'tinyint default NULL')) {
			array_push($updates,"ALTER TABLE $adr_table CHANGE adr_custom_".$id." adr_custom_".$id." ".pnVarPrepForStore($modType[$k]));
		}
	}

	$error = '';

	if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','updateCustomfields',array('updates'=>$updates))) {
    	if (empty($error)) { $error .= 'UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		else { $error .= ' - UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
	}

	if (count($modDel)) {
		$deletes = array();
		foreach($modDel as $k=>$id) {
				array_push($deletes,"DELETE FROM $cus_table WHERE $cus_column[nr] = $id");
				if (($modDelType[$k] != 'smallint default NULL') && ($modDelType[$k] != 'tinyint default NULL')) {
					array_push($deletes,"ALTER TABLE $adr_table DROP adr_custom_".$id);
				}
		}
    	if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','deleteCustomfields',array('deletes'=>$deletes))) {
    		if (empty($error)) { $error .= 'DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
    	}
	}
	if (isset($newtype) && ($newtype == 'tinyint default NULL')) {
		$newname = '[      ]';
	}
	if (isset($newtype) && ($newtype == 'smallint default NULL')) {
		$newname = '[------]';
	}
	if( (isset($newname)) && ($newname != '') ) {
		list($dbconn) = pnDBGetConn();
		$result = $dbconn->Execute("SELECT MAX($cus_column[nr]) FROM $cus_table");
		list($nextID) = $result->fields;
		$nextID++;
		$result->close;
		$inserts = array();
		array_push($inserts,"INSERT INTO $cus_table ($cus_column[nr],$cus_column[name],$cus_column[type],$cus_column[position])
                              VALUES ($nextID,'".pnVarPrepForStore($newname)."','".pnVarPrepForStore($newtype)."',9999999999)");
		if (($newtype != 'smallint default NULL') && ($newtype != 'tinyint default NULL')) {
			array_push($inserts,"ALTER TABLE $adr_table ADD adr_custom_".$nextID." ".pnVarPrepForStore($newtype));
		}

        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','addCustomfields',array('inserts'=>$inserts))) {
            if (empty($error)) { $error .= 'INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		}
    }

	$args=array('msg'=>$error);

	pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'customfields',$args));
	return true;
}
//=========================================================================
//  Decrement position for a custom field
//=========================================================================
function pnAddressBook_admin_decCustomfields()
{
    // Get parameters
    $id = pnVarCleanFromInput('id');

    // Confirm authorisation code
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'main'));
        return true;
    }

    // Pass to API
    if (pnModAPIFunc(__PNADDRESSBOOK__, 'admin', 'decCustomFields', array('id' => $id))) {
        // Success
        //pnSessionSetVar('statusmsg', _BLOCKLOWER);
    }

    // Redirect
    pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin',  'customfields'));
    return true;
}
//=========================================================================
//  Increment position for a custom field
//=========================================================================
function pnAddressBook_admin_incCustomfields()
{
    // Get parameters
    $id = pnVarCleanFromInput('id');

    // Confirm authorisation code
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'main'));
        return true;
    }

    // Pass to API
    if (pnModAPIFunc(__PNADDRESSBOOK__, 'admin', 'incCustomFields', array('id' => $id))) {
        // Success
        //pnSessionSetVar('statusmsg', _BLOCKLOWER);
    }

    // Redirect
    pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin',  'customfields'));
    return true;
}
//=========================================================================
//  Prefix field form
//=========================================================================
function pnAddressBook_admin_prefixes($args) {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }
    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$msg = pnVarCleanFromInput('msg');
	if ($msg) { $output->Text('<div align="center">'.$msg.'</div>');}
	else {$output->Linebreak(1); }

    $pres = pnModAPIFunc(__PNADDRESSBOOK__,'admin','getPrefixes');
    if(!is_array($pres)) {
        $output->Text($pres);
        return $output->GetOutput();
    }

	// Start form
    $output->FormStart(pnModURL(__PNADDRESSBOOK__, 'admin', 'updateprefixes'));

    // Add an authorisation ID
    $output->FormHidden('authid', pnSecGenAuthKey());

	$output->Text(pnAddressBook_themetable('start'));
	$output->Linebreak(1);
	$output->Text('<table align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_PREFIXLABEL).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_CAT_DELETE).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	foreach($pres as $pre) {
		$output->TableRowStart();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormText('name[]',$pre['name'],20,30);
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormHidden('id[]',$pre['nr']);
		$output->FormCheckbox('del[]',false,$pre['nr']);
		$output->TableColEnd();
		$output->TableRowEnd();
	}

	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('newname','',20,30);
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(_pnAB_CAT_NEW);
	$output->TableColEnd();
	$output->TableRowEnd();

	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('end'));

	// End form
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
    $output->FormSubmit(pnVarPrepHTMLDisplay(_pnAB_PNADDRESSBOOKUPDATE));
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	// Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Prefix field update
//=========================================================================
function pnAddressBook_admin_updateprefixes() {

	$output = new pnHTML();

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	list($id,$del,$name,$newname) = pnVarCleanFromInput('id','del','name','newname');
	if(is_array($del)) {
        $dels = implode(',',$del);
    }

	$modID = $modName = array();

	if(isset($id)) {
		foreach($id as $k=>$i) {
        	$found = false;
        	if(count($del)) {
            	foreach($del as $d) {
                	if($i == $d) {
                    	$found = true;
                    	break;
                	}
            	}
        	}
        	if(!$found) {
            	array_push($modID,$i);
            	array_push($modName,$name[$k]);
            }
    	}
	}

	$pntable = pnDBGetTables();
	$pre_table = $pntable[pnaddressbook_prefixes];
	$pre_column = $pntable['pnaddressbook_prefixes_column'];

	$updates = array();
    foreach($modID as $k=>$id) {
    array_push($updates,"UPDATE $pre_table
                             SET $pre_column[name]='".pnVarPrepForStore($modName[$k])."'
                             WHERE $pre_column[nr]=$id");
	}

	$error = '';

	if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','updatePrefixes',array('updates'=>$updates))) {
    	if (empty($error)) { $error .= 'UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		else { $error .= ' - UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
	}

	$delete = "DELETE FROM $pre_table WHERE $pre_column[nr] IN ($dels)";
	if(isset($dels)) {
        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','deletePrefixes',array('delete'=>$delete))) {
            if (empty($error)) { $error .= 'DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
        }
    }

	if( (isset($newname)) && ($newname != '') ) {
        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','addPrefixes',array('name'=>$newname))) {
            if (empty($error)) { $error .= 'INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		}
    }

	$args=array('msg'=>$error);

	pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'prefixes',$args));
	return true;
}

//=========================================================================
//  Label form
//=========================================================================
function pnAddressBook_admin_labels($args) {
	global $bgcolor1,$bgcolor2,$bgcolor3,$bgcolor4;

    $output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text(pnAddressBook_themetable('start'));

	// some design ;)
	$bc1 = $bgcolor1;
	$bc2 = $bgcolor2;
	if ($bgcolor1 == $bgcolor2) {
		if ($bgcolor1 == $bgcolor3) {
			$bc2 = $bgcolor4;
		}
		else {
			$bc2 = $bgcolor3;
		}
	}

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

    $output->Text(pnAddressBook_admin_menu());
	$output->Text(pnAddressBook_themetable('end'));
	$msg = pnVarCleanFromInput('msg');
	if ($msg) { $output->Text('<div align="center">'.$msg.'</div>');}
	else {$output->Linebreak(1); }

    $labels = pnModAPIFunc(__PNADDRESSBOOK__,'admin','getLabels');
    if(!is_array($labels)) {
        $output->Text($labels);
        return $output->GetOutput();
    }

	// Start form
    $output->FormStart(pnModURL(__PNADDRESSBOOK__, 'admin', 'updatelabels'));

    // Add an authorisation ID
    $output->FormHidden('authid', pnSecGenAuthKey());

	$output->Text(pnAddressBook_themetable('start'));
	$output->Linebreak(1);
	$output->Text('<table align="center" cellpadding="5" cellspacing="1" bgcolor="'.$bc2.'">');
	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_LAB_NAME).'</b>');
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text('<b>'.pnVarPrepHTMLDisplay(_pnAB_LAB_DELETE).'</b>');
	$output->TableColEnd();
	$output->TableRowEnd();

	foreach($labels as $label) {
		$output->TableRowStart();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormText('name[]',$label['name'],20,30);
		$output->TableColEnd();
		$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
		$output->FormHidden('id[]',$label['nr']);
		$output->FormCheckbox('del[]',false,$label['nr']);
		$output->TableColEnd();
		$output->TableRowEnd();
	}

	$output->TableRowStart();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->FormText('newname','',20,30);
	$output->TableColEnd();
	$output->Text('<td align="center" valign="middle" bgcolor="'.$bc1.'">');
	$output->Text(_pnAB_LAB_NEW);
	$output->TableColEnd();
	$output->TableRowEnd();

	$output->TableRowEnd();
	$output->Text('</table>');
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('end'));

	// End form
	$output->Linebreak(1);
	$output->Text(pnAddressBook_themetable('start'));
	$output->Text('<div align="center"><br>');
    $output->FormSubmit(pnVarPrepHTMLDisplay(_pnAB_PNADDRESSBOOKUPDATE));
	$output->Text('<br><br></div>');
	$output->Text(pnAddressBook_themetable('end'));
    $output->FormEnd();

	// Return the output that has been generated by this function
    $output->SetInputMode(_PNH_PARSEINPUT);
	return $output->GetOutput();
}

//=========================================================================
//  Labels update
//=========================================================================
function pnAddressBook_admin_updatelabels() {

	$output = new pnHTML();

    // Security check
    if (!pnSecAuthAction(0, 'pnAddressBook::', '::', ACCESS_ADMIN)) {
        $output->Text(pnVarPrepHTMLDisplay(_PNADDRESSBOOK_NOAUTH));
        $output->Text(pnAddressBook_themetable('end'));
		return $output->GetOutput();
    }

	list($id,$del,$name,$newname) = pnVarCleanFromInput('id','del','name','newname');
	if(is_array($del)) {
        $dels = implode(',',$del);
    }

	$modID = $modName = array();

	if(isset($id)) {
		foreach($id as $k=>$i) {
        	$found = false;
        	if(count($del)) {
            	foreach($del as $d) {
                	if($i == $d) {
                    	$found = true;
                    	break;
                	}
            	}
        	}
        	if(!$found) {
            	array_push($modID,$i);
            	array_push($modName,$name[$k]);
            }
    	}
	}

	$pntable = pnDBGetTables();
	$lab_table = $pntable[pnaddressbook_labels];
	$lab_column = $pntable['pnaddressbook_labels_column'];

	$updates = array();
    foreach($modID as $k=>$id) {
    array_push($updates,"UPDATE $lab_table
                             SET $lab_column[name]='".pnVarPrepForStore($modName[$k])."'
                             WHERE $lab_column[nr]=$id");
	}

	$error = '';

	if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','updatelabels',array('updates'=>$updates))) {
        if (empty($error)) { $error .= 'UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
		else { $error .= ' - UPDATE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
    }

	$delete = "DELETE FROM $lab_table WHERE $lab_column[nr] IN ($dels)";
	if(isset($dels)) {
        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','deletelabels',array('delete'=>$delete))) {
            if (empty($error)) { $error .= 'DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - DELETE '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
        }
    }

	if( (isset($newname)) && ($newname != '') ) {
        if(pnModAPIFunc(__PNADDRESSBOOK__,'admin','addlabels',array('name'=>$newname))) {
            if (empty($error)) { $error .= 'INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
			else { $error .= ' - INSERT '.pnVarPrepHTMLDisplay(_pnAB_SUCCESS); }
        }
    }

	if ($error > 0) {
		$msg = pnVarPrepHTMLDisplay(_ERROR);
	}
	else {
		$msg = pnVarPrepHTMLDisplay(_pnAB_SUCCESS);
	}

	$args=array('msg'=>$error);
	pnRedirect(pnModURL(__PNADDRESSBOOK__, 'admin', 'labels',$args));
	return true;
}

//=========================================================================
//  Main Menu
//=========================================================================
function pnAddressBook_admin_menu() {

   	$settingsURL	= pnModURL(__PNADDRESSBOOK__,'admin','modifyconfig');
	$categoryURL	= pnModURL(__PNADDRESSBOOK__,'admin','categories');
	$labelURL		= pnModURL(__PNADDRESSBOOK__,'admin','labels');
	$prefixURL		= pnModURL(__PNADDRESSBOOK__,'admin','prefixes');
	$customURL		= pnModURL(__PNADDRESSBOOK__,'admin','customfields');
//START - gehunter
	$exportURL		= pnModURL(__PNADDRESSBOOK__,'user','export');
//END - gehunter

	$settingsTXT	=pnVarPrepHTMLDisplay(_pnAB_EDIT_CONFIG);
	$categoryTXT	=pnVarPrepHTMLDisplay(_pnAB_EDIT_pnAB_CATEGORY);
	$labelTXT		=pnVarPrepHTMLDisplay(_pnAB_EDIT_LABEL);
	$prefixTXT		=pnVarPrepHTMLDisplay(_pnAB_EDIT_PREFIX);
	$customTXT		=pnVarPrepHTMLDisplay(_pnAB_EDIT_CUSTOM);
//START - gehunter
	$exportTXT		=pnVarPrepHTMLDisplay(_pnAB_ADMIN_EXPORT);
//END - gehunter

	$output = new pnHTML();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Title(pnVarPrepHTMLDisplay(_PNADDRESSBOOK));
	$output->Text('<div align="center">');
	$output->Text(pnAddressBook_themetable('start',2));
	$output->Text('<div align="center">');
	$output->URL($settingsURL,$settingsTXT);
	$output->Text(' | ');
    $output->URL($categoryURL,$categoryTXT);
	$output->Text(' | ');
	$output->URL($labelURL,$labelTXT);
	$output->Text(' | ');
	$output->URL($prefixURL,$prefixTXT);
	$output->Text(' | ');
	$output->URL($customURL,$customTXT);
	$output->Text(' | ');
	$output->URL($exportURL,$exportTXT);
	$output->Text('</div>');
	$output->Text(pnAddressBook_themetable('end',2));
	$output->Text('</div>');
	$output->Linebreak(1);

    // Return the output that has been generated by this function
    return $output->GetOutput();
}

//=========================================================================
//  Open/Close tables like the current theme
//=========================================================================
function pnAddressBook_themetable($option, $type=1) {
	$theme = pnUserGetTheme();
	pnThemeLoad($theme);
    $output = new pnHTML();
	ob_start();
	if ($option == 'start') {
		if ($type == 1) { OpenTable(); }
		else { OpenTable2(); }
	}
	else {
		if ($type == 1) { CloseTable(); }
		else { CloseTable2(); }
	}
	$table = ob_get_contents();
	ob_end_clean();
	$output->SetInputMode(_PNH_VERBATIMINPUT);
	$output->Text($table);

	return $output->GetOutput();
}
?>